package icl.exercise;

import icl.exercise.managers.AccelerometerListener;
import icl.exercise.managers.AccelerometerManager;
import icl.exercise.map.MapViewActivity;
import icl.exercise.utilities.FileUtilities;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ExerciseActivity extends Activity implements AccelerometerListener {

	private static Context CONTEXT;

	FileUtilities FileUtils = new FileUtilities();

	private Button btn_map;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		CONTEXT = this;

		this.btn_map = (Button) findViewById(R.id.BTN_MAP);

		this.btn_map.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				openGoogleMapsActivity();

			}

		});

		// Acquire a reference to the system Location Manager
		LocationManager locationManager = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);

		// Define a listener that responds to location updates
		LocationListener locationListener = new LocationListener() {
			public void onLocationChanged(Location location) {
				// Called when a new location is found by the network location
				// provider.
				makeUseOfNewLocation(location);
			}

			private void makeUseOfNewLocation(Location location) {
				((TextView) findViewById(R.id.TV_LAT)).setText("Latitude: "
						+ location.getLatitude());
				((TextView) findViewById(R.id.TV_LONG)).setText("Longitude: "
						+ location.getLongitude());

				FileUtils.addNewGPSValue(location);

			}

			public void onStatusChanged(String provider, int status,
					Bundle extras) {
			}

			public void onProviderEnabled(String provider) {
			}

			public void onProviderDisabled(String provider) {
			}
		};

		// Register the listener with the Location Manager to receive location
		// updates
		locationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
	}

	protected void onResume() {
		super.onResume();
		if (AccelerometerManager.isSupported()) {
			AccelerometerManager.startListening(this);
		}
	}

	protected void onDestroy() {
		super.onDestroy();
		if (AccelerometerManager.isListening()) {
			AccelerometerManager.stopListening();
		}

	}

	public static Context getContext() {
		return CONTEXT;
	}

	/**
	 * onShake callback
	 */
	public void onShake(float force) {
		((TextView) findViewById(R.id.TV_X))
				.setText(("Phone shaked : " + force));
		// Toast.makeText(this, "Phone shaked : " + force, 1000).show();
	}

	/**
	 * onAccelerationChanged callback
	 */
	public void onAccelerationChanged(float x, float y, float z) {
		((TextView) findViewById(R.id.TV_X)).setText("X: " + String.valueOf(x));
		((TextView) findViewById(R.id.TV_Y)).setText("Y: " + String.valueOf(y));
		((TextView) findViewById(R.id.TV_Z)).setText("Z: " + String.valueOf(z));

		FileUtils.addNewAccelerometerValue(x, y, z);
	}

	public void openGoogleMapsActivity() {
		Intent i = new Intent(this, MapViewActivity.class);

		String fromLat = ((TextView) findViewById(R.id.TV_LAT)).getText()
				.toString();
		String fromLong = ((TextView) findViewById(R.id.TV_LONG)).getText()
				.toString();

		/* Sending some arguments */
		Bundle bundle = new Bundle();

		bundle.putString("fromLat", fromLat);
		bundle.putString("fromLong", fromLong);

		i.putExtras(bundle);

		startActivity(i);
	}
}